/**
 * 构建召唤兽数据
 * @param {*} pet_name 召唤兽名称
 */
export function build_pet_data(pet_name) {
    const pet_data = {
        "参战等级": 0,
        "等级": 1,
        "名称": pet_name,
        "模型": pet_name,
        "气血": 0,
        "魔法": 0,
        "最大气血": 0,
        "最大魔法": 0,
        "伤害": 0,
        "防御": 0,
        "速度": 0,
        "灵力": 0,
        "体质": 20,
        "魔力": 20,
        "力量": 20,
        "耐力": 20,
        "敏捷": 20,
        "潜力": 0,
        "忠诚": 100,
        "寿命": G.RandomUtils.generate_random(5000, 7000),
        "装备": [],
        "技能": [],
        "种类": "宝宝",
        "五行": G.RandomUtils.random_wx(),
        "染色组": 0,
        "参战信息": 0,
        "装备属性": {
            "气血": 0,
            "魔法": 0,
            "命中": 0,
            "伤害": 0,
            "防御": 0,
            "速度": 0,
            "躲避": 0,
            "灵力": 0,
            "体质": 0,
            "魔力": 0,
            "力量": 0,
            "耐力": 0,
            "敏捷": 0,
        },
        "特性五维": {
            "力量": 0,
            "敏捷": 0,
            "耐力": 0,
            "魔力": 0,
            "体质": 0,
        },
        "进阶属性": {
            "力量": 0,
            "敏捷": 0,
            "耐力": 0,
            "魔力": 0,
            "体质": 0,
        },
        "当前经验": 0,
        "最大经验": 0,
        "自动": null,
        "默认技能": null,
        "认证码": `${Date.now()}_${G.RandomUtils.generate_random(100, 999)}_${G.RandomUtils.generate_random(100, 999)}`,
        "内丹": { "上限": 6, "数据": [] },
        "元宵": { "可用": 10, "如意丹": 1000, "千金露": 4, "水晶糕": 20, "炼兽真经": 5, "元宵时间": 0, "真经时间": 0 },
        "封印命中等级": 0,
        "抵抗封印等级": 0,
    };
    let pet_basic_data = G.PetData.all_pet_data[pet_name];
    if (pet_basic_data === undefined) {
        pet_basic_data = G.PetData.get_randmon_pet_data();
    }
    if (pet_basic_data !== undefined) {
        pet_data["攻击资质"] = pet_basic_data["攻击资质"];
        pet_data["防御资质"] = pet_basic_data["防御资质"];
        pet_data["体力资质"] = pet_basic_data["体力资质"];
        pet_data["法力资质"] = pet_basic_data["法力资质"];
        pet_data["躲闪资质"] = pet_basic_data["躲闪资质"];
        pet_data["速度资质"] = pet_basic_data["速度资质"];
        pet_data["参战等级"] = pet_basic_data["参战等级"];
        pet_data["技能"] = pet_basic_data["技能"];
        pet_data["成长"] = G.RandomUtils.random_get_array(pet_basic_data["成长"]);
        pet_data["最大经验"] = G.PetData.get_pet_level_exp(pet_data["等级"]);
        pet_data["寿命"] = pet_basic_data["寿命"] || pet_data["寿命"];
        pet_data["种类"] = pet_basic_data["种类"] || pet_data["种类"];
    }
    computing_pet_data(pet_data);
    return pet_data;
}

/**
 * 计算特性对五维的影响
 */
function computing_five_dimensional_attributes(pet_data) {
    pet_data["特性五维"] = {};
    let five_dimensional_data = 0;
    if (pet_data["进阶"] !== undefined && pet_data["进阶"]["开启"]) {
        let tx_list = ["自恋", "预知", "灵动", "识物", "洞察", "乖巧"];
        if (tx_list.includes(pet_data["进阶"]["特性"])) {
            five_dimensional_data = pet_data["进阶"]["特性属性"][1];
        }
    }
    pet_data["特性五维"]["力量"] = five_dimensional_data * -1;
    pet_data["特性五维"]["敏捷"] = five_dimensional_data * -1;
    pet_data["特性五维"]["耐力"] = five_dimensional_data * -1;
    pet_data["特性五维"]["魔力"] = five_dimensional_data * -1;
    pet_data["特性五维"]["体质"] = five_dimensional_data * -1;
}


export function computing_pet_data(pet_data) {
    //计算特性对五维的影响
    computing_five_dimensional_attributes(pet_data);
    // 	if self.种类==="野怪" and self.yeguai===nil then
    // 	   self:重置野怪属性()
    // 	end
    let tmp_tz = pet_data["体质"] + pet_data["装备属性"]["体质"] + pet_data["特性五维"]["体质"] + pet_data["进阶属性"]["体质"];
    let tmp_ml = pet_data["魔力"] + pet_data["装备属性"]["魔力"] + pet_data["特性五维"]["魔力"] + pet_data["进阶属性"]["魔力"];
    let tmp_ll = pet_data["力量"] + pet_data["装备属性"]["力量"] + pet_data["特性五维"]["力量"] + pet_data["进阶属性"]["力量"];
    let tmp_nl = pet_data["耐力"] + pet_data["装备属性"]["耐力"] + pet_data["特性五维"]["耐力"] + pet_data["进阶属性"]["耐力"];
    let tmp_mj = pet_data["敏捷"] + pet_data["装备属性"]["敏捷"] + pet_data["特性五维"]["敏捷"] + pet_data["进阶属性"]["敏捷"];
    pet_data["最大气血"] = Math.ceil((pet_data["等级"] * pet_data["体力资质"] / 1000) + (tmp_tz * pet_data["成长"] * 6) + pet_data["装备属性"]["气血"]);
    pet_data["最大魔法"] = Math.ceil((pet_data["等级"] * pet_data["法力资质"] / 500) + (tmp_ml * pet_data["成长"] * 3) + pet_data["装备属性"]["魔法"]);
    pet_data["伤害"] = Math.ceil((pet_data["等级"] * pet_data["攻击资质"] * (13.8 + 10 * pet_data["成长"]) / 7500) + (tmp_ll * pet_data["成长"]) + pet_data["装备属性"]["伤害"]);
    pet_data["防御"] = Math.ceil((pet_data["等级"] * pet_data["防御资质"] * (4.8 + 10 * pet_data["成长"]) / 7500) + (tmp_nl * pet_data["成长"] * 1.33) + pet_data["装备属性"]["防御"]);
    pet_data["速度"] = Math.ceil((pet_data["速度资质"] * tmp_mj / 1000) + pet_data["装备属性"]["速度"]);
    pet_data["灵力"] = Math.ceil((pet_data["等级"] * pet_data["法力资质"] / 3500) + (pet_data["等级"] * 0.4 + tmp_tz * 0.3 + tmp_nl * 0.2 + tmp_ll * 0.4 + tmp_ml * 0.65) + pet_data["装备属性"]["灵力"]);
    pet_data["躲避"] = Math.ceil((pet_data["速度资质"] * tmp_mj / 1000) + pet_data["装备属性"]["躲避"]);

    //增加气血上限
    if (pet_data["技能"].includes("净台妙谛")) {
        pet_data["最大气血"] += Math.floor(pet_data["体质"] * pet_data["成长"] * 2);
    }
    if (pet_data["技能"].includes("欣欣向荣") && pet_data["五行"] === "木") {
        pet_data["最大气血"] += Math.floor(pet_data["最大气血"] * 1.4);
    }

    //降低气血上限
    if (pet_data["进阶"] !== undefined && pet_data["进阶"]["开启"]) {
        if (["瞬击", "瞬法", "灵断"].includes(pet_data["进阶"]["特性"])) {
            pet_data["最大气血"] = Math.floor(pet_data["最大气血"] * (1 - pet_data["进阶"]["特性属性"][1] * 0.01));
        }
    }

    if (pet_data["技能"].includes("高级强力")) {
        pet_data["伤害"] += Math.floor(pet_data["等级"] * 0.55);
    }
    if (pet_data["技能"].includes("强力")) {
        pet_data["伤害"] += Math.floor(pet_data["等级"] * 0.4);
    }
    if (pet_data["技能"].includes("超级防御")) {
        pet_data["防御"] += Math.floor(pet_data["等级"]);
    }
    if (pet_data["技能"].includes("高级防御")) {
        pet_data["防御"] += Math.floor(pet_data["等级"] * 0.8);
    }
    if (pet_data["技能"].includes("防御")) {
        pet_data["防御"] += Math.floor(pet_data["等级"] * 0.6);
    }
    if (pet_data["技能"].includes("逍遥游")) {
        // 提升自身20%速度
        pet_data["速度"] += Math.floor(pet_data["速度"] * 0.2);
    }
    if (pet_data["技能"].includes("溜之大吉")) {
        pet_data["速度"] += Math.floor(pet_data["速度"] * 0.2);
    }
    if (pet_data["技能"].includes("高级敏捷")) {
        pet_data["速度"] += Math.floor(pet_data["速度"] * 0.2);
    }
    if (pet_data["技能"].includes("敏捷")) {
        pet_data["速度"] += Math.floor(pet_data["速度"] * 0.1);
    }
    if (pet_data["技能"].includes("迟钝")) {
        pet_data["速度"] -= Math.floor(pet_data["速度"] * 0.2);
    }
    // 内丹属性加成
    for (let index = 0; index < pet_data["内丹"]["数据"].length; index++) {
        let nei_dan = pet_data["内丹"]["数据"][index];
        let nei_dan_data = G.NeiDanData.default[nei_dan["技能"]];
        let characters_id = pet_data["认证码"].split("_")[0];
        let base = 1 + (nei_dan["等级"] * 0.2) + pet_data["成长"];
        let ty = get_pet_tong_yu(Number(characters_id), pet_data["认证码"])
        // 如果设置了坐骑统御则加上坐骑成长
        if (ty) {
            base = base + ty[0] - 1;
        }
        if (nei_dan_data && nei_dan_data["召唤兽属性加成"]) {
            nei_dan_data["召唤兽属性加成"](pet_data, base);
        }
    }

    pet_data["气血"] = Math.floor(pet_data["最大气血"]);
    pet_data["魔法"] = Math.floor(pet_data["最大魔法"]);
    pet_data["法伤"] = Math.floor(pet_data["灵力"]);
    pet_data["法防"] = Math.floor(pet_data["灵力"]);
}


/**
 * 取召唤兽统御数据
 * @param {*} characters_id 
 * @param {*} pet_number 
 * @returns 
 */
export function get_pet_tong_yu(characters_id, pet_number) {
    let characters = _CharactersData.get(characters_id);
    for (let index = 0; index < characters["坐骑列表"].length; index++) {
        let mounts = characters["坐骑列表"][index];
        if (mounts) {
            for (let j = 0; j < mounts["统御召唤兽"].length; j++) {
                let ty_pet_number = mounts["统御召唤兽"][j];
                if (ty_pet_number === pet_number) {
                    return [mounts["成长"], mounts["坐骑技能"]];
                }
            }
        }
    }
    return undefined;
}

/**
 * 添加召唤兽经验
 * @param {*} pet_data 
 * @param {*} exp 
 * @returns 
 */
export function add_exp(pet_data, exp) {
    let characters_id = pet_data["认证码"].split("_")[0];
    let characters = _CharactersData.get(Number(characters_id));
    if (pet_data["等级"] - characters["等级"] >= 5) {
        ClientUtils.send_characters_basic_prompt(characters_id, "你的召唤兽当前等级已经超过人物等级+5，目前已经无法再获得更多的经验了。");
        return;
    }
    pet_data["当前经验"] += exp;
    G.ChatUtils.send_character_system_chat(characters_id, `#W/你的${pet_data["名称"]}#W/获得了${exp}点经验`);
    if (pet_data["当前经验"] > pet_data["最大经验"]) {
        auto_up_level(pet_data);
    }
    ClientUtils.send_characters_client_message(characters_id, 16, characters["召唤兽"]);
}

function auto_up_level(pet_data) {
    while (pet_data["当前经验"] >= pet_data["最大经验"] && pet_data["等级"] < max_level) {
        pet_data["当前经验"] = pet_data["当前经验"] - pet_data["最大经验"];
        pet_data["最大经验"] = G.PetData.get_pet_level_exp(pet_data["等级"]);
        pet_data["等级"] += 1;
        pet_data["体质"] += 1;
        pet_data["魔力"] += 1;
        pet_data["力量"] += 1;
        pet_data["耐力"] += 1;
        pet_data["敏捷"] += 1;
        pet_data["潜力"] += 5;
        //跟随等级提升的属性 TODO 进阶特性
        if (pet_data["进阶"] !== undefined && pet_data["进阶"]["开启"]) {
            if (pet_data["进阶"]["特性"] === "力破") {
                pet_data["进阶"]["特性属性"][1] += 1;
            }
            if (pet_data["进阶"]["怒吼"] === "力破") {
                pet_data["进阶"]["特性属性"][0] += 1;
            }
        }
    }
    computing_pet_data(pet_data);
    let characters_id = pet_data["认证码"].split("_")[0];
    let characters = _CharactersData.get(Number(characters_id));
    G.ChatUtils.send_character_system_chat(characters_id, `#W/你的#R/${pet_data["名称"]}#W/等级提升到了#R/${pet_data["等级"]}#W/级`);
    if (pet_data["参战信息"] === 1) {
        let pet_index = 0;
        for (let index = 0; index < characters["召唤兽"].length; index++) {
            let element = characters["召唤兽"][index];
            if (element["认证码"] === pet_data["认证码"]) {
                pet_index = index;
                break;
            }
        }
        G.Characters.update_fight_pet(characters, pet_data, pet_index);
        ClientUtils.send_characters_client_message(characters_id, 18, characters["参战宝宝"]);
    }
}

/**
 * 穿装备
 * @param {*} characters_id 角色id
 * @param {*} pet_data 召唤兽
 * @param {*} key 背包类型
 * @param {*} index 装备在背包内的位置
 * @returns 
 */
export function wear_equipment(characters_id, pet_data, key, index) {

    let characters = _CharactersData.get(characters_id);
    let character_client_id = G.Characters.get_characters_client_id(characters_id);
    let characters_items = characters[key];
    if (characters_items === undefined) {
        if (character_client_id !== undefined) {
            ClientUtils.send_basic_prompt(character_client_id, `#R角色的${key}背包不存在`);
        }
        return false;
    }
    let item = characters_items[index];
    if (item === undefined || item === null) {
        ClientUtils.send_client_message(character_client_id, `#R${key}背包内不存在该物品`);
        return false;
    }
    let equipment_type_data = G.ItemUtils.get_item_equipment_type(item);
    //装备位置类型
    let equipment_master_type = equipment_type_data[0];
    if (equipment_master_type !== "召唤兽") {
        if (character_client_id !== undefined) {
            //通知客户端更新信息
            ClientUtils.send_basic_prompt(character_client_id, "不是召唤兽装备");
            return;
        }
    }
    //装备位置
    let equipment_index = Number(equipment_type_data[2]);
    //装备总位置大小
    let equipment_index_size = Number(equipment_type_data[3]);
    //所有装备
    if (pet_data["装备"] === undefined || pet_data["装备"].length === 0) {
        //初始化装备位置
        pet_data["装备"] = [];
        for (let index = 0; index < equipment_index_size; index++) {
            pet_data["装备"].push(null);
        }
    }

    //已装备或为空都替换背包原来的位置

    let equipment = {};
    if (pet_data["装备"][equipment_index] === undefined || pet_data["装备"][equipment_index] === null) {
        equipment = null
    } else {
        Object.assign(equipment, pet_data["装备"][equipment_index]);
    }

    let new_equipment = {};
    Object.assign(new_equipment, item);

    //更新背包
    characters_items[index] = equipment;
    characters[key] = characters_items;

    //穿戴装备到指定位置
    pet_data["装备"][equipment_index] = new_equipment;

    let attributes = ["速度", "躲避", "伤害", "灵力", "防御", "气血"];
    if (attributes.includes(new_equipment["宝石属性"])) {
        pet_data["装备属性"][new_equipment["宝石属性"]] += new_equipment["宝石属性"];
    }
    pet_data["装备属性"]["气血"] += (new_equipment["气血"] || 0);
    pet_data["装备属性"]["魔法"] += (new_equipment["魔法"] || 0);
    pet_data["装备属性"]["命中"] += (new_equipment["命中"] || 0);
    pet_data["装备属性"]["伤害"] += (new_equipment["伤害"] || 0);
    pet_data["装备属性"]["防御"] += (new_equipment["防御"] || 0);
    pet_data["装备属性"]["速度"] += (new_equipment["速度"] || 0);
    pet_data["装备属性"]["躲避"] += (new_equipment["躲避"] || 0);
    pet_data["装备属性"]["体质"] += (new_equipment["体质"] || 0);
    pet_data["装备属性"]["魔力"] += (new_equipment["魔力"] || 0);
    pet_data["装备属性"]["力量"] += (new_equipment["力量"] || 0);
    pet_data["装备属性"]["耐力"] += (new_equipment["耐力"] || 0);
    pet_data["装备属性"]["敏捷"] += (new_equipment["敏捷"] || 0);

    computing_pet_data(pet_data);

    //通知客户端更新信息
    if (character_client_id !== undefined) {
        ClientUtils.send_client_message(character_client_id, 3699, 1);
        ClientUtils.send_client_message(character_client_id, 20, pet_data);
        ClientUtils.send_client_message(character_client_id, 28, 1);
    }
}

/**
 * 脱下装备
 * @param {*} characters_id 
 * @param {*} pet_data 
 * @param {*} key 背包类型
 * @param {*} index 
 */
export function take_off_equipment(characters_id, pet_data, key, index) {
    let characters = _CharactersData.get(characters_id);
    if (pet_data["装备"][index] !== undefined) {
        let clone_item = Object.assign({}, pet_data["装备"][index]);
        //放入背包
        let items = characters[key];
        G.ItemsUils.add_item_to_items(items, pet_data["装备"][index], 1);
        characters[key] = items;
        //移除装备位
        pet_data["装备"][index] = null;


        let attributes = ["速度", "躲避", "伤害", "灵力", "防御", "气血"];
        if (attributes.includes(clone_item["宝石属性"])) {
            pet_data["装备属性"][clone_item["宝石属性"]] += clone_item["宝石属性"];
        }
        pet_data["装备属性"]["气血"] -= (clone_item["气血"] || 0);
        pet_data["装备属性"]["魔法"] -= (clone_item["魔法"] || 0);
        pet_data["装备属性"]["命中"] -= (clone_item["命中"] || 0);
        pet_data["装备属性"]["伤害"] -= (clone_item["伤害"] || 0);
        pet_data["装备属性"]["防御"] -= (clone_item["防御"] || 0);
        pet_data["装备属性"]["速度"] -= (clone_item["速度"] || 0);
        pet_data["装备属性"]["躲避"] -= (clone_item["躲避"] || 0);
        pet_data["装备属性"]["体质"] -= (clone_item["体质"] || 0);
        pet_data["装备属性"]["魔力"] -= (clone_item["魔力"] || 0);
        pet_data["装备属性"]["力量"] -= (clone_item["力量"] || 0);
        pet_data["装备属性"]["耐力"] -= (clone_item["耐力"] || 0);
        pet_data["装备属性"]["敏捷"] -= (clone_item["敏捷"] || 0);

        G.Pet.computing_pet_data(pet_data);
        let character_client_id = G.Characters.get_characters_client_id(characters_id);
        //通知客户端更新信息
        if (character_client_id !== undefined) {
            ClientUtils.send_client_message(character_client_id, 3699, 1);
            ClientUtils.send_client_message(character_client_id, 20, pet_data);
            ClientUtils.send_client_message(character_client_id, 28, 1);
        }
    }
}

// 召唤兽丸子赐福技能
export const pet_cf_skills = ["必杀", "毒", "盾气", "法术暴击", "法术波动", "法术抵抗", "法术连击", "反击", "反震", "防御", "飞行", "否定信仰", "感知", "合纵", "慧根", "火属性吸收", "进阶必杀", "进阶法暴", "精神集中", "雷属性吸收", "连击", "敏捷", "冥思", "魔之心", "强力", "驱鬼", "神迹", "水属性吸收", "偷袭", "土属性吸收", "吸血", "幸运", "夜战", "遗志", "隐身", "永恒", "再生", "招架", "奔雷咒", "壁垒击破", "地狱烈火", "水漫金山", "泰山压顶"];

/**
 * 寻找指定模型的召唤兽下标
 * @param {*} characters_id 
 * @param {*} model 
 */
export function pet_index_of_model(characters_id, model) {
    let characters = _CharactersData.get(characters_id);
    for (let index = 0; index < characters["召唤兽"].length; index++) {
        let element = characters["召唤兽"][index];
        if (element["模型"] === model) {
            return index;
        }
    }
}